<template>
    <transition name="slide">
        <div>
            <city-header></city-header>
            <city-search :cities="cities" @select="handleCityClick"></city-search>
            <city-list :letter="letter" :cities="cities" :hot="hotCities" @select="handleCityClick"></city-list>
            <city-alphabet :cities="cities" @change="handleLetterChange"></city-alphabet>
        </div>
    </transition>
</template>


<script>
import axios from "axios";
import CityHeader from "./components/Header";
import CitySearch from "./components/Search";
import CityList from "./components/List";
import CityAlphabet from "./components/Alphabet";

import { mapGetters, mapMutations } from "vuex";
export default {
  components: {
    CityHeader,
    CitySearch,
    CityList,
    CityAlphabet
  },
  created() {
    this.getCityInfo();
  },
  data() {
    return {
      cities: {},
      hotCities: [],
      letter: ""
    };
  },
  methods: {
    getCityInfo() {
      axios.get("/api/city.json").then(res => {
        console.log(res);
        res = res.data;
        if (res.ret && res.data) {
          const data = res.data;
          this.cities = data.cities;
          this.hotCities = data.hotCities;
        }
      });
    },

    handleCityClick(city) {
      console.log("bbb");
      this.changeCity(city);
      this.$router.back();
    },
    ...mapMutations({
      changeCity: "changeCity"
    }),

    handleLetterChange(letter) {
      console.log(letter);
      this.letter = letter;
    }
  }
};
</script>

<style scoped lang="stylus" rel="stylesheet/stylus" >
.slide-enter-active, .slide-leave-active {
  transition: all 0.3s;
}

.slide-enter, .slide-leave-to {
  transform: translate3d(100%, 0, 0);
}
</style>
